﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XNAGuiLib
{
    public class TextureObject
    {
        #region Field - Position
        protected Vector2 _position = new Vector2();
        /// <summary>
        /// gets or sets TextObjects position on screen
        /// </summary>
        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }
        #endregion

        #region Field - RelativePosition
        protected Vector2 _relativePosition = new Vector2();
        /// <summary>
        /// gets or sets TextObjects position on screen
        /// </summary>
        public Vector2 RelativePosition
        {
            get
            {
                return _relativePosition;
            }
            set
            {
                _relativePosition = value;
            }
        }
        #endregion

        #region Field - Bounds
        protected Vector2 _bounds = new Vector2();
        /// <summary>
        /// gets or sets TextObjects bounds (width, height) on screen
        /// </summary>
        public Vector2 Bounds
        {
            get
            {
                return _bounds;
            }
            set
            {
                _bounds = value;
            }
        }
        #endregion

        #region Field - Width
        /// <summary>
        /// gets or sets Width
        /// </summary>
        public float Width
        {
            get
            {
                return _bounds.X;
            }
            set
            {
                _bounds = new Vector2(value, _bounds.Y);
            }
        }
        #endregion

        #region Field - Height
        /// <summary>
        /// gets or sets Height
        /// </summary>
        public float Height
        {
            get
            {
                return _bounds.Y;
            }
            set
            {
                _bounds = new Vector2(_bounds.X, value);
            }
        }
        #endregion

        #region Field - X
        /// <summary>
        /// gets or sets x Position
        /// </summary>
        public float X
        {
            get
            {
                return _relativePosition.X;
            }
            set
            {
                _relativePosition = new Vector2(value, _relativePosition.Y);
            }
        }
        #endregion

        #region Field - Y
        /// <summary>
        /// gets or sets y Position
        /// </summary>
        public float Y
        {
            get
            {
                return _relativePosition.Y;
            }
            set
            {
                _relativePosition = new Vector2(_relativePosition.X, value);
            }
        }
        #endregion



        #region Field - Texture
        protected Texture2D _texture;
        /// <summary>
        /// gets or sets the TextureObjects Texture2D
        /// </summary>
        public Texture2D Texture
        {
            get
            {
                return _texture;
            }
            set
            {
                _texture = value;
            }
        }
        #endregion

        #region Field - Color
        protected Color _color = Color.White;
        /// <summary>
        /// gets or sets the TextureObjects Color
        /// </summary>
        public Color Color
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
            }
        }
        #endregion

        #region Field - Rotation
        protected float _rotation = 0.0f;
        /// <summary>
        /// gets or sets the TextureObjects drawing rotation
        /// </summary>
        public float Rotation
        {
            get
            {
                return _rotation;
            }
            set
            {
                _rotation = value;
            }
        }
        #endregion

        #region Field - Origin
        private Vector2 _origin = new Vector2(0.0f, 0.0f);
        /// <summary>
        /// gets the Origin of this TextureObjects
        /// </summary>
        public Vector2 Origin
        {
            get
            {
                return _origin;
            }
        }
        #endregion

        #region Field - Effects
        protected SpriteEffects _effects = SpriteEffects.None;
        /// <summary>
        /// gets or sets the TextureObjects SpriteEffects
        /// </summary>
        public SpriteEffects Effects
        {
            get
            {
                return _effects;
            }
            set
            {
                _effects = value;
            }
        }
        #endregion

        #region Field - LayerDepth
        protected float _layerDepth = 1.0f;
        /// <summary>
        /// gets or sets the TextureObjects layer depth (between 0.0 (front) and 1.0 (back))
        /// </summary>
        public float LayerDepth
        {
            get
            {
                return _layerDepth;
            }
            set
            {
                _layerDepth = value;
            }
        }
        #endregion

        #region Field - Scale
        protected Vector2 _scale = new Vector2(1.0f, 1.0f);
        /// <summary>
        /// gets or sets the TextObject scale
        /// </summary>
        public Vector2 Scale
        {
            get
            {
                return _scale;
            }
            set
            {
                _scale = value;
                Console.WriteLine(this.Name + " -> " + _scale);
                Console.WriteLine(this.Name + " -> " + _bounds.X * _scale.X);
            }
        }
        #endregion

        #region Field - Name
        protected string _name = "";
        /// <summary>
        /// gets or sets the TextureObjects name
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        #endregion

        #region Field - Visible
        protected bool _visible = true;
        /// <summary>
        /// gets or sets if the Draw-Method should be executed
        /// </summary>
        public bool Visible
        {
            get
            {
                return _visible;
            }
            set
            {
                _visible = value;
            }
        }
        #endregion


        #region Constructor - TextureObject()
        /// <summary>
        /// only used to provide DummyObjects
        /// </summary>
        public TextureObject()
        {

        }
        #endregion

        #region Constructor - TextureObject(Texture2D texture, string name, Vector2 relPosition, Vector2 bounds)
        public TextureObject(Texture2D texture, string name, Vector2 relPosition, Vector2 bounds)
        {
            // set the name
            _name = name;
            // set the Texture2D
            _texture = texture;
            // set the destination (relative to the InterfaceObjects absolut position)
            _relativePosition = relPosition;
            _bounds = bounds;
        }
        #endregion

        #region Constructor - TextureObject(Texture2D texture, string name, Vector2 relPosition, Vector2 bounds, bool visible)
        public TextureObject(Texture2D texture, string name, Vector2 relPosition, Vector2 bounds, bool visible)
        {
            // set the name
            _name = name;
            // set the Texture2D
            _texture = texture;
            // set the destination (relative to the InterfaceObjects absolut position)
            _relativePosition = relPosition;
            _bounds = bounds;
            // set if TextureObject is visible
            _visible = visible;
        }
        #endregion


        public bool IsInitialized()
        {
            if (_texture != null)
                return true;
            else return false;
        }


        #region Method - CheckCoordinates(MouseState ms)
        public bool CheckCoordinates(MouseState ms)
        {
            return new Rectangle((int)_relativePosition.X + (int)_position.X, (int)_relativePosition.Y + (int)_position.Y, (int)_bounds.X, (int)_bounds.Y).Intersects(new Rectangle(ms.X, ms.Y, 1, 1));
        }
        #endregion

        #region Method - Draw(SpriteBatch sprite)
        public void Draw(SpriteBatch sprite)
        {
            int x = (int)(_position.X + (int)_relativePosition.X);
            int y = (int)(_position.Y + (int)_relativePosition.Y);
            int w = (int)(_bounds.X * _scale.X);
            int h = (int)(_bounds.Y * _scale.Y);

            if (_visible) sprite.Draw(_texture, new Rectangle(x, y, w, h), null, _color, _rotation, _origin, _effects, _layerDepth);
        }
        #endregion

        #region Override Method - ToString()
        public override string ToString()
        {
            return _name;
        }
        #endregion
    }
}